package hrm.action.login;

import hrm.action.Action;
import hrm.action.ActionForward;
import hrm.dto.insa.EmployeeDTO;
import hrm.dto.login.LoginDTO;
import hrm.serviceimple.EmployeeServiceImple;
import hrm.serviceimple.LoginServiceImple;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BeforeInsertLogin implements Action {

	@SuppressWarnings("unchecked")
	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		ActionForward actfoward = new ActionForward();
		EmployeeServiceImple employeeService = EmployeeServiceImple.getInstance();
		LoginServiceImple loginService = LoginServiceImple.getInstance();
		
		EmployeeDTO dto = new EmployeeDTO();
		String id = request.getParameter("id");
		String namekor = request.getParameter("namekor");
		dto.setId(id);
		dto.setNamekor(namekor);
		
		List employeeList = employeeService.getAllEmployee(dto);
		request.setAttribute("employeeList", employeeList);
		
		LoginDTO loginDTO = new LoginDTO();
		loginDTO.setId(id);
		
		if (loginService.getLoginCount(loginDTO) > 0) {
			loginDTO = loginService.getLoginByID(loginDTO);
		}else{
			loginDTO.setId(id);
			loginDTO.setName(namekor);
		}
		request.setAttribute("loginDTO", loginDTO);
		actfoward.setUrl("loginInsert");
		return actfoward;
	}

}
